<?php
// 用户模型
class MonthwaModel extends CommonModel {
	//自动填充设置
    protected $_auto = array(
        array('riqi', 'strtotime', 3, 'function'),//1新增时，2更新时
        array('username', 'getUserName', 3, 'callback'),//1新增时，2更新时
        array('userid', 'getMemberId', 3, 'callback'),//1新增时，2更新时
        array('week', 'getWeek', 3, 'callback'),//1新增时，2更新时
        array('createtime', 'time', 1, 'function'),//1新增时，2更新时
    );
	//自动验证
    protected $_validate         =         array(  
        array('id','checkLimit','没有修改权限！',1,'callback',2), // 自定丿凼数验证密码格式 
        array('riqi,limits','checkPreCount1','当天教研活动安排数量超限！',1,'callback',1), // 
        array('id,riqi,limits','checkPreCount2','当天教研活动安排数量超限！',1,'callback',2), // 
        array('id,riqi','checkDeathLine','当前日期不可添加或修改活动安排！',1,'callback',3), // 
        array('address','require','地址必须！'), 
    ); 

    //检查用户权限，用户只可以修改自己录入的数据。
    protected function checkLimit($data){
        if($_SESSION[C('USER_AUTH_KEY')] == $this->where('id='.$data)->getField('userid')){
            return True;
        }else {
            $this->error =  L('_OPERATION_WRONG_');//  "数据锁定，不可修改！";
            return False;
        }
    }
    //用于检查当天的项目是否超过两个，1为添加时，2为编辑时，编辑时只增加是否修改了时间的判断，其他一样
    protected function checkPreCount1($data){
        $monwa  = M('Monthwa');
        if($data['limits']=='中小学'){
            $count1  = $monwa->where('riqi='.strtotime($data['riqi'])." and limits in('".$data['limits']."','小学')")->count();
            $count2  = $monwa->where('riqi='.strtotime($data['riqi'])." and limits in('".$data['limits']."','中学')")->count();
            $count   = $count1>$count2?$count1:$count2;
        }else{
            $count  = $monwa->where('riqi='.strtotime($data['riqi'])." and limits in('".$data['limits']."','中小学')")->count();
        }
        if($count>=C('MONTH_WA_MAX')){
            return False;
        }else{
            return True;
        }
    }
    protected function checkPreCount2($data){
        if(strtotime($data['riqi']) == $this->where('id='.$data['id'])->getField('riqi')){
            return true;
        }else{
            $monwa  = M('Monthwa');
            if($data['limits']=='中小学'){
                $count1  = $monwa->where('riqi='.strtotime($data['riqi'])." and limits in('".$data['limits']."','小学')")->count();
                $count2  = $monwa->where('riqi='.strtotime($data['riqi'])." and limits in('".$data['limits']."','中学')")->count();
                $count   = $count1>$count2?$count1:$count2;
            }else{
                $count  = $monwa->where('riqi='.strtotime($data['riqi'])." and limits in('".$data['limits']."','中小学')")->count();
            }
            if($count>=C('MONTH_WA_MAX')){
                return False;
            }else{
                return True;
            }
        }//if,else 
    }
    protected function checkDeathLine($data){
        $curtime    = date(time());
        $edittime   = strtotime($data['riqi']);        
        $day0       = getthemonth($edittime);
        //$day1       = getthemonth($curtime);
        //$dc         = ($day[1]-$day[0])/60/60/24+1;//当前月的总天数
        $monthStart = $day0[0];
        $monthEnd   = $day0[1];
        /*$d = intval(date('d'));
        $m = intval(date('m'));
        $y = intval(date('Y'));*/
        if(strpos("-,".C('UNLIMITE_USER').",-",','.$_SESSION['loginUserName'].',')>=1){
            return true;
        }
        if($curtime<=($monthStart+C('MONTH_WA_DELAY'))){
            return true;
        }
        return false;
       /* if($d<=intval(C('MONTH_WA_DEATHLINE'))){
            if($m==12){
                $first = ($y+1).'-01-01';
            }else{
                $first = $y.'-'.($m+1).'-01';
            }
            $day = getthemonth($first);

            if(strtotime($data['riqi'])>=$day[0] && strtotime($data['riqi'])<=$day[1]){
                return true;
            }else{
                return False;
            }
        }else{
            //echo intval(C('MONTH_WA_DEATHLINE'));
            return False;
        }   
        return False;*/
    }
}
?>